window.addEvent('domready', function(){
	var form = $('frmSearch');
	
	//pagination
	new Pagination($('detachiev_pagination'),{
		onPageChange: function(obj){
			form.offset.value = obj.getPageNumber()-1;
			search_cont(form);
		}
	});	
	
	//table header untuk sorting
	new SortableTableHeader($('detachiev_table').getElement('tr[class:header]'), {
		tableFieldName:['detachiev_id','detachiev_nama', 'detachiev_tingkat', 'achiev_jenis','achiev_bidang'],
		onHeaderClick:function(item, fieldName, sort){
			form.orderby.value = fieldName+" "+sort;
			search_cont(form);
		}
	});
	
	search(form);	
});

function search(form){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			$('detachiev_table_content').set('html', json.table_content);
			
			//rubah jumlah halamannya
			$('detachiev_pagination').retrieve('pagination').setPageCount(Math.ceil(json.table_num_record/10));
		}
	}).send();
}

function search_cont(form){
	form.set('send',{
		onSuccess: function(res){
			//table content
			var json = JSON.decode(res);
			$('detachiev_table_content').set('html', json.table_content);
		}
	}).send();
}

function do_add(){
	FBModal_show2(APP.base+'/ajax/addModal', 'post', "", true, true, null, {
		onSuccess: function(){
			new FormCheck('frmModal',{submitByAjax:true, 
		        onAjaxSuccess: function(res){
					var json = JSON.decode(res);
					FBModal_message(APP.base+'/ajax/messageDialog', "Save", json.msg);
					search($('frmSearch'));
		        }
		    });
			
			//install combo
			$$("select[name='inp[1]']").addEvent('change', function(){
				//bidang
				var target = $$("select[name='inp[2]']")[0];
				var noll = target.options[0].clone();
				target.empty().grab(noll);
				if (!this.value) return 
				ajax(APP.base+'/ajax/getComboBidang/'+this.value, "post", "", $empty, function(res){
					target.set('html',target.get('html')+res);
				});
				
				//tingkat
				var target2 = $$("select[name='inp[3]']")[0];
				var noll2 = target.options[0].clone();
				target2.empty().grab(noll2);
				if (!this.value) return 
				ajax(APP.base+'/ajax/getComboTingkat/'+this.value, "post", "", $empty, function(res){
					target2.set('html',target2.get('html')+res);
				});
			});
		}
	});
}

function do_delete(rowElement, id){
	ajax(APP.base+'/ajax/delete/'+id, "post", "", $empty, function(res){
		var json = JSON.decode(res);
		if (!json.error){
			//tween opacity and destroy it
			rowElement.get('tween').chain(function(){
				rowElement.destroy();
			}).start('opacity',[1,0]);
		}
		FBModal_message(APP.base+'/ajax/messageDialog', "Delete", json.msg);
		search($('frmSearch'));
	});
}

function do_edit(id){
	FBModal_show2(APP.base+'/ajax/editModal/'+id, 'post', "", true, true, null, {
		onSuccess: function(){
			new FormCheck('frmModal',{submitByAjax:true, 
		        onAjaxSuccess: function(res){
					var json = JSON.decode(res);
					FBModal_message(APP.base+'/ajax/messageDialog', "Save", json.msg);
					search($('frmSearch'));
		        }
		    });
			
			//install combo
			$$("select[name='inp[1]']").addEvent('change', function(){
				//bidang
				var target = $$("select[name='inp[2]']")[0];
				var noll = target.options[0].clone();
				target.empty().grab(noll);
				if (!this.value) return 
				ajax(APP.base+'/ajax/getComboBidang/'+this.value, "post", "", $empty, function(res){
					target.set('html',target.get('html')+res);
				});
				
				//tingkat
				var target2 = $$("select[name='inp[3]']")[0];
				var noll2 = target.options[0].clone();
				target2.empty().grab(noll2);
				if (!this.value) return 
				ajax(APP.base+'/ajax/getComboTingkat/'+this.value, "post", "", $empty, function(res){
					target2.set('html',target2.get('html')+res);
				});
			});
		}
	});
}